package com.longge.wechat.dao.impl;

import java.util.List;




import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.longge.wechat.dao.ShopDao;
import com.longge.wechat.dao.BaseDao;
import com.longge.wechat.entity.ShopEntity;
import com.longge.wechat.utils.SignUtil;

@Repository
public class ShopDaoImpl extends BaseDao<ShopEntity> implements ShopDao {

	@Override
	public void saveShop(ShopEntity entity) {
		super.save(entity);
	}

	@Override
	public void deleteShop(ShopEntity entity) {
		super.delete(entity);
	}

	@Override
	public void updateShop(ShopEntity entity) {
		super.update(entity);
	}

	@Override
	public ShopEntity findShopById(int id) {
		return super.get(id);
	}

	@Override
	public List<ShopEntity> findAllShop(String name,int pageSize, int currentPage) {
		String hql = "from ShopEntity se where 1=1";
		if(SignUtil.isNotEmpty(name)) {
			hql +=" and se.name like '%"+name+"%'";
		}
		hql +=" order by se.priority desc";
		return super.getObjects(pageSize, currentPage, hql);
	}

	@Override
	public int findAllCount() {
		String hql = "from ShopEntity";
		return super.findAllCount(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ShopEntity> findAllShopBySpecialtyId(Integer specialtyId,
			int pageSize, int currentPage) {
		String hql = "select new com.longge.wechat.entity.ShopEntity(se.id,se.path,se.name,se.address,se.tel,se.addTime,se.priority,se.hours)"
				+ " from ShopEntity se,SpecialtyShopEntity sse "
				+ "where sse.shopId = se.id";
		if(specialtyId != null) {
			hql += " and sse.specialtyId ="+specialtyId;
		}
		hql += " order by se.priority desc,se.addTime desc";
		Query query = super.createQuery(hql);
		query.setMaxResults(pageSize);
		query.setFirstResult(currentPage);
		return query.list();
	}

}
